﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;

namespace Citi.Helpers
{
    public class FileHelper
    {
        public static string DateFormat { get; set; }

        public static void WriteTxtFile<T>(IEnumerable<T> models, Dictionary<string,string> headers, string separator, string path, 
            string fileName, string networkUser = null, string networkPassword = null)
        {
            using (NetworkShareAccesser.Access(path, networkUser, networkPassword))
            {
                fileName = string.Concat(fileName, ".txt");

                //File.CreateText(Path.Combine(path, fileName));

                var stringBuilder = new StringBuilder();
                
                foreach (var header in headers)
                {
                    if (headers.ToList().IndexOf(header) != headers.Count() - 1)
                    {
                        stringBuilder.Append(header.Value);
                        stringBuilder.Append(separator);
                    }
                    else
                        stringBuilder.AppendLine(header.Value);
                }

                foreach (var model in models)
                {
                    foreach (var header in headers)
                    {
                        var propertyInfo = model.GetType().GetProperty(header.Key.Replace("*", ""));

                        var value = propertyInfo.PropertyType == typeof (DateTime) ||
                                    propertyInfo.PropertyType == typeof (DateTime?)
                                        ? ((DateTime?) propertyInfo.GetValue(model, null)).HasValue
                                              ? ((DateTime) propertyInfo.GetValue(model, null)).ToString(DateFormat)
                                              : string.Empty
                                        : propertyInfo.GetValue(model, null);

                        if (headers.ToList().IndexOf(header) != headers.Count() - 1)
                        {
                            stringBuilder.Append(value);
                            stringBuilder.Append(separator);
                        }
                        else
                            stringBuilder.AppendLine(value.ToString());
                    }
                }

                File.AppendAllText(Path.Combine(path, fileName), stringBuilder.ToString());
            }
        }

        public static void CompressFile(string fileToZip, string fileZipToSave)
        {
            using (var zip = new ZipFile())
            {
                zip.AddFile(fileToZip, "");
                zip.Save(fileZipToSave);
            }
        }

        //public static void Compress(FileInfo fi)
        //{
        public static void Compress(string pathToZip, string pathToSave)
        {
            using (var zip = new ZipFile())//.Read(pathToZip))
            {
                zip.AddFile(pathToZip,"");
                zip.Save(Path.Combine(pathToSave, string.Concat(Path.GetFileNameWithoutExtension(pathToZip), ".zip")));
                //foreach (ZipEntry e in zip)
                //    e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
            }
            //using (FileStream inFile = fi.OpenRead())
            //{
            //    if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fi.Extension != ".gz")
            //    {
            //        using (FileStream outFile = File.Create(fi.FullName + ".gz"))
            //        {
            //            using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
            //                inFile.CopyTo(Compress);
            //        }
            //    }
            //}
        }

        public static void Decompress(string zipToUnpack, string unpackDirectory)
        {
            using (var zip = ZipFile.Read(zipToUnpack))
            {
                foreach (ZipEntry e in zip)
                    e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
            }

            //using (FileStream inFile = fi.OpenRead())
            //{
            //    string curFile = fi.FullName;
            //    string origName = curFile.Remove(curFile.Length - fi.Extension.Length);

            //    using (FileStream outFile = File.Create(origName))
            //    {
            //        using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress))
            //            Decompress.CopyTo(outFile);
            //    }
            //}
        }

        public static string MakeValidFileName(string name)
        {
            var builder = new StringBuilder();
            var invalid = System.IO.Path.GetInvalidFileNameChars();
            foreach (var cur in name)
            {
                if (!invalid.Contains(cur))
                    builder.Append(cur);
            }
            return builder.ToString();
        }
    }
}

